home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / Apache 1.0 / src / mod_log_referer.c < prev    next >
Text File  |  1995-12-04  |  7KB  |  225 lines

  1.  
  2. /* ====================================================================
  3.  * Copyright (c) 1995 The Apache Group.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * 1. Redistributions of source code must retain the above copyright
  10.  *    notice, this list of conditions and the following disclaimer. 
  11.  *
  12.  * 2. Redistributions in binary form must reproduce the above copyright
  13.  *    notice, this list of conditions and the following disclaimer in
  14.  *    the documentation and/or other materials provided with the
  15.  *    distribution.
  16.  *
  17.  * 3. All advertising materials mentioning features or use of this
  18.  *    software must display the following acknowledgment:
  19.  *    "This product includes software developed by the Apache Group
  20.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  21.  *
  22.  * 4. The names "Apache Server" and "Apache Group" must not be used to
  23.  *    endorse or promote products derived from this software without
  24.  *    prior written permission.
  25.  *
  26.  * 5. Redistributions of any form whatsoever must retain the following
  27.  *    acknowledgment:
  28.  *    "This product includes software developed by the Apache Group
  29.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  30.  *
  31.  * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
  32.  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  33.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  34.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
  35.  * IT'S CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  39.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  42.  * OF THE POSSIBILITY OF SUCH DAMAGE.
  43.  * ====================================================================
  44.  *
  45.  * This software consists of voluntary contributions made by many
  46.  * individuals on behalf of the Apache Group and was originally based
  47.  * on public domain software written at the National Center for
  48.  * Supercomputing Applications, University of Illinois, Urbana-Champaign.
  49.  * For more information on the Apache Group and the Apache HTTP server
  50.  * project, please see <http://www.apache.org/>.
  51.  *
  52.  */
  53.  
  54.  
  55.  
  56. #include "httpd.h"
  57. #include "http_config.h"
  58.  
  59. #define DEFAULT_REFERERLOG "logs/referer_log"
  60.  
  61. module referer_log_module;
  62.  
  63. static int xfer_flags = ( O_WRONLY | O_APPEND | O_CREAT );
  64. static mode_t xfer_mode = ( S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH );
  65.  
  66. typedef struct {
  67.     char *fname;
  68.     int referer_fd;
  69.     array_header *referer_ignore_list;
  70. } referer_log_state;
  71.  
  72. void *make_referer_log_state (pool *p, server_rec *s)
  73. {
  74.     referer_log_state *cls =
  75.       (referer_log_state *)palloc (p, sizeof (referer_log_state));
  76.  
  77.     cls->fname = DEFAULT_REFERERLOG;
  78.     cls->referer_fd = -1;
  79.     cls->referer_ignore_list = make_array(p, 1, sizeof(char *));
  80.     return (void *)cls;
  81. }
  82.  
  83. char *set_referer_log (cmd_parms *parms, void *dummy, char *arg)
  84. {
  85.     referer_log_state *cls = get_module_config (parms->server->module_config,
  86.                            &referer_log_module);
  87.   
  88.     cls->fname = arg;
  89.     return NULL;
  90. }
  91.  
  92. char *add_referer_ignore (cmd_parms *parms, void *dummy, char *arg)
  93. {
  94.   char **addme;
  95.   referer_log_state *cls = get_module_config (parms->server->module_config,
  96.                           &referer_log_module);
  97.  
  98.   addme = push_array(cls->referer_ignore_list);
  99.   *addme = pstrdup(cls->referer_ignore_list->pool, arg);
  100.   return NULL;
  101. }
  102.  
  103. command_rec referer_log_cmds[] = {
  104. { "RefererLog", set_referer_log, NULL, RSRC_CONF, TAKE1,
  105.     "the filename of the referer log" },
  106. { "RefererIgnore", add_referer_ignore, NULL, RSRC_CONF, ITERATE,
  107.     "referer hostnames to ignore" },
  108. { NULL }
  109. };
  110.  
  111. void referer_log_child (void *cmd)
  112. {
  113.     /* Child process code for 'RefererLog "|..."';
  114.      * may want a common framework for this, since I expect it will
  115.      * be common for other foo-loggers to want this sort of thing...
  116.      */
  117.     
  118.     cleanup_for_exec();
  119.     signal (SIGHUP, SIG_IGN);
  120.     execl (SHELL_PATH, SHELL_PATH, "-c", (char *)cmd, NULL);
  121.     fprintf (stderr, "Exec of shell for logging failed!!!\n");
  122.     exit (1);
  123. }
  124.  
  125. void open_referer_log (server_rec *s, pool *p)
  126. {
  127.     referer_log_state *cls = get_module_config (s->module_config,
  128.                            &referer_log_module);
  129.   
  130.     char *fname = server_root_relative (p, cls->fname);
  131.     
  132.     if (cls->referer_fd > 0) return; /* virtual log shared w/main server */
  133.     
  134.     if (*cls->fname == '|') {
  135.     FILE *dummy;
  136.     
  137.     spawn_child(p, referer_log_child, (void *)(cls->fname+1),
  138.             kill_after_timeout, &dummy, NULL);
  139.  
  140.     if (dummy == NULL) {
  141.         fprintf (stderr, "Couldn't fork child for RefererLog process\n");
  142.         exit (1);
  143.     }
  144.  
  145.     cls->referer_fd = fileno (dummy);
  146.     }
  147.     else if((cls->referer_fd = popenf(p, fname, xfer_flags, xfer_mode)) < 0) {
  148.         fprintf(stderr,"httpd: could not open transfer log file %s.\n", fname);
  149.         perror("open");
  150.         exit(1);
  151.     }
  152. }
  153.  
  154. void init_referer_log (server_rec *s, pool *p)
  155. {
  156.     for (; s; s = s->next) open_referer_log (s, p);
  157. }
  158.  
  159. int referer_log_transaction(request_rec *orig)
  160. {
  161.     char **ptrptr, **ptrptr2;
  162.     referer_log_state *cls = get_module_config (orig->server->module_config,
  163.                            &referer_log_module);
  164.   
  165.     char str[HUGE_STRING_LEN];
  166.     char *referer;
  167.     request_rec *r;
  168.  
  169.     if(cls->referer_fd <0)
  170.       return OK;
  171.  
  172.     for (r = orig; r->next; r = r->next)
  173.         continue;
  174.     
  175.     referer = table_get(orig->headers_in, "Referer");
  176.     if(referer != NULL)
  177.       {
  178.  
  179.  
  180.       /* The following is an upsetting mess of pointers, I'm sorry
  181.          Anyone with the motiviation and/or the time should feel free
  182.          to make this cleaner... */
  183.  
  184.       ptrptr2 = (char **) (cls->referer_ignore_list->elts + 
  185.              (cls->referer_ignore_list->nelts *
  186.               cls->referer_ignore_list->elt_size));
  187.       
  188.       /* Go through each element of the ignore list and compare it to the
  189.          referer_host.  If we get a match, return without logging */
  190.  
  191.       for(ptrptr = (char **) cls->referer_ignore_list->elts;
  192.           ptrptr < ptrptr2;
  193.           ptrptr += cls->referer_ignore_list->elt_size) 
  194.         {
  195.         if(strstr(referer, *ptrptr))
  196.           return OK;
  197.         }
  198.       
  199.       
  200.       sprintf(str, "%s -> %s\n", referer,
  201.           r->uri);
  202.       write(cls->referer_fd, str, strlen(str));
  203.       }
  204.  
  205.     return OK;
  206. }
  207.  
  208. module referer_log_module = {
  209.    STANDARD_MODULE_STUFF,
  210.    init_referer_log,        /* initializer */
  211.    NULL,            /* create per-dir config */
  212.    NULL,            /* merge per-dir config */
  213.    make_referer_log_state,    /* server config */
  214.    NULL,            /* merge server config */
  215.    referer_log_cmds,        /* command table */
  216.    NULL,            /* handlers */
  217.    NULL,            /* filename translation */
  218.    NULL,            /* check_user_id */
  219.    NULL,            /* check auth */
  220.    NULL,            /* check access */
  221.    NULL,            /* type_checker */
  222.    NULL,            /* fixups */
  223.    referer_log_transaction    /* logger */
  224. };
  225.